*Recoding ZA5270

use "Data\ZA5270\ZA5270_v2-0-0.dta" , clear

*Support for democracy
gen $idea_democracy				= pd12

*Support for liberal democracy
gen $libdem_opposition			= pd07a
gen $libdem_freeopinion			= pd05a
gen $libdem_partyopportunity	= pd08a
gen $libdem_criticism			= pd06a							
gen $libdem_conflict			= pd01a							
gen $libdem_generalwill			= pd09a

*Satisfaction with democracy
gen $satisfaction_democracy		= ps03

*Vertrauen Institutionen
gen $trust_parties = pt15
gen $trust_bundestag = pt03
gen $trust_government = pt12
gen $trust_verfassungsgericht = pt02
gen $trust_justice = pt08
gen $trust_administration = pt04

*Efficacy
gen $politicians_takecare = pe01
gen $politicians_interestpublic = pe05
gen $ego_politicdiscuss = pe02
gen $ego_politicscomplicated = pe04

*define missing values
mvdecode $idea_democracy $libdem_opposition $libdem_freeopinion $libdem_partyopportunity ///
	$libdem_criticism $libdem_conflict $libdem_generalwill ///
		$politicians_takecare $politicians_interestpublic $ego_politicdiscuss $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration, ///
	mv(-9=.a\-8=.b)  //.a = no answer; .b= = don't know 


*Standardize range from 0 to 1; higher values indicate higher support for democracy
*reverse scale if necessary and standardize

foreach var of varlist $idea_democracy $libdem_opposition $libdem_freeopinion $libdem_partyopportunity $satisfaction_democracy {
	replace `var'	= (6-`var')/5 if !missing(`var')
}

*standardize remaining items 

foreach var of varlist $libdem_criticism $libdem_conflict $libdem_generalwill {
	replace `var'	=(`var'-1)/4 if !missing(`var')
}


foreach var of varlist $trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration {
	replace `var' = (`var' - 1)/6 if !missing(`var')
}

foreach var of varlist $politicians_takecare $ego_politicscomplicated {
	replace `var' = (`var' - 1)/3 if !missing(`var')
}

foreach var of varlist $politicians_interestpublic $ego_politicdiscuss {
	replace `var' = (4 - `var')/3 if !missing(`var')
}



*Sociodemographic Controls
gen			$gender 			= sex-1

// age already defined; only drop label
label drop 						age											//remove value label


gen 		$education			= educ
replace 	$education 			= 0 if $education != 5 						 	// still at school, none, Hautpschule, Mittlere Reife, Fachhochschulreife = 0
replace 	$education 			= 1 if $education == 5							// Hochschulreife = 1

gen 		$eastwest 				= eastwest-1									 

*Weight
gen weight = wghtpew


*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(pa04, 1, 3, 42)
replace $partyid_category	= 0 	if inlist(pa04, 2, 4, 6)
replace $partyid_category	= -1 	if inlist(pa04, 90)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(pv01, 1, 3, 42)
replace $partyvote_category	= 0 	if inlist(pv01, 2, 4, 6)
replace $partyvote_category	= -1 	if inlist(pv01, 90)

*Birthplace
*Did R live mosly in east/ west in his/her youth?
* 1: east, 0: west 
gen $born_eastwest			= 1		if inlist(dg03, 1, 2)
replace $born_eastwest		= 0		if inlist(dg03, 3, 4)


*Meta variables
gen $dataset 					= "ZA5270"
gen $year 						= 2018


*check
tab $idea_democracy pd12, m
tab $libdem_opposition pd07a, m
tab $libdem_freeopinion pd05a, m
tab $libdem_partyopportunity pd08a, m
tab $libdem_criticism pd06a, m						
tab $libdem_conflict pd01a, m					
tab $libdem_generalwill pd09a, m	
tab $satisfaction_democracy ps03, m 
tab $gender sex, m
tab $age, m
tab $education educ, m
tab $eastwest eastwest, m	

*Save recode dataset
keep $idea_democracy $libdem_opposition	$libdem_freeopinion 					///
	$libdem_partyopportunity $libdem_criticism	$libdem_conflict 				///
	$politicians_takecare $politicians_interestpublic $ego_politicdiscuss $ego_politicscomplicated ///
	$trust_parties $trust_bundestag $trust_government $trust_verfassungsgericht $trust_justice $trust_administration ///
	$libdem_generalwill $satisfaction_democracy $gender $age $education $eastwest $dataset $year  weight	$partyid_category	$partyvote_category	
save ZA5270_recoded.dta, replace
